/******************************************************************************
* Copyright (C) 2018, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file main.c
 **
 ** A detailed description is available at
 ** @link Sample Group Some description @endlink
 **
 **   - 2019-03-26  1.0  Lux First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/
#include <stdint.h>
#include "app_task.h"
#include "bsp_init.h"
#include <includes.h>
#include "hc32_ll.h"
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
OS_SEM 		SemPrint;

#define LED_TASK_STK_SIZE 	128

static CPU_STK  LEDTask_STK[LED_TASK_STK_SIZE];
static OS_TCB   LEDTask_TCB;

#define PRINT_TASK_STK_SIZE 128
static CPU_STK  PrintTask_STK[PRINT_TASK_STK_SIZE];
static OS_TCB   PrintTask_TCB;
/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/


/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
void Task_LED_Entry(void * parameter)
{
  OS_ERR os_err;
  static uint16_t LED_Run_Cycle = 0;
  while (1)
  {
    OSSemPend( (OS_SEM  *)&SemPrint,				///< 等待信号量
               (OS_TICK  )0,                                    ///< 
               (OS_OPT	 )OS_OPT_PEND_BLOCKING,
               (CPU_TS  *)0,
               (OS_ERR	*)&os_err);
    printf("\nuCOS-III LED Task Run Cycle: %d \r\n",++LED_Run_Cycle);
		
    OSSemPost( (OS_SEM	*)&SemPrint,
               (OS_OPT	 )OS_OPT_POST_ALL,
               (OS_ERR	*)&os_err);
    OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, &os_err);
  }
}


void Task_Print_Entry(void)
{
  OS_ERR os_err;
  static uint16_t Print_Run_Cycle = 0;
  while(1)
  {
    OSSemPend( (OS_SEM	*)&SemPrint,                             ///< 等待信号量
               (OS_TICK	 )0,                                     ///< 
               (OS_OPT	 )OS_OPT_PEND_BLOCKING,
               (CPU_TS  *)0,
               (OS_ERR	*)&os_err);
    printf("\nuCOS-III Print Task Run Cycle: %d \r\n",++Print_Run_Cycle);
		
    OSSemPost( (OS_SEM	*)&SemPrint,
               (OS_OPT	 )OS_OPT_POST_ALL,
               (OS_ERR	*)&os_err);
    OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &os_err);
  }
}  

extern OS_TCB  AppTaskStartTCB;
void AppTaskstart(void* p_arg)
{
  CPU_INT32U  cpu_clk_freq;
  CPU_INT32U  cnts;
  OS_ERR Err;
  (void)p_arg;
	
  LL_PERIPH_WE(LL_PERIPH_ALL);
	
  BSP_CLK_Init();
	
  Debug_Uart_Init();
	
  CPU_Init();
	
  LL_PERIPH_WP(LL_PERIPH_ALL);
	
  printf("\nSystem Begin \r\n");
	
  cpu_clk_freq = SystemCoreClock;                    /* Determine SysTick reference freq */
  cnts  = cpu_clk_freq/(CPU_INT32U)OSCfg_TickRate_Hz;/* Determine nbr SysTick increments */          
  OS_CPU_SysTickInit(cnts);
	
  Mem_Init(); 		///< 初始化内存管理组件
	
#if OS_CFG_STAT_TASK_EN > 0u
  OSStatTaskCPUUsageInit(&Err);                     /* Compute CPU capacity with no task running */
#endif

  OSSemCreate((OS_SEM           *)&SemPrint,
              (CPU_CHAR	        *)"Print Sem",
              (OS_SEM_CTR       )1,
              (OS_ERR           *)&Err);
	
  OSTaskCreate((OS_TCB	        *)&LEDTask_TCB,           /*Task TCB*/
               (CPU_CHAR        *)"LED_Task", 	          /*task Name*/
               (OS_TASK_PTR      )Task_LED_Entry,         /*Stack Entry*/
               (void            *)NULL,                   /*Task Parameter*/
               (OS_PRIO	         )4,                      /*Task Poirity*/
               (CPU_STK         *)&LEDTask_STK[0],        /*Stack Start*/
               (CPU_STK          )LED_TASK_STK_SIZE/10,   /*Stack Limit*/
               (CPU_STK          )LED_TASK_STK_SIZE,      /*Stack Size*/
               (OS_MSG_QTY       )5u,
               (OS_TICK	         )2u,
               (void            *)0u,
               (OS_OPT	         )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
               (OS_ERR	        *)&Err);
				 
  OSTaskCreate((OS_TCB	        *)&PrintTask_TCB,         /*Task TCB*/
               (CPU_CHAR        *)"Print_Task",           /*task Name*/
               (OS_TASK_PTR      )Task_Print_Entry,       /*Stack Entry*/
               (void            *)NULL,                   /*Task Parameter*/
               (OS_PRIO	         )5,                      /*Task Poirity*/
               (CPU_STK         *)&PrintTask_STK[0],      /*Stack Start*/
               (CPU_STK          )PRINT_TASK_STK_SIZE/10, /*Stack Limit*/
               (CPU_STK          )PRINT_TASK_STK_SIZE, 	  /*Stack Size*/
               (OS_MSG_QTY       )0u,
               (OS_TICK	         )0u,
               (void            *)0u,
               (OS_OPT           )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
               (OS_ERR          *)&Err);
  OSTaskDel(&AppTaskStartTCB, &Err);
}
/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
